package com.ubeecube.mylocker.common;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import com.ubeecube.mylocker.utility.Constants;
import com.ubeecube.mylocker.utility.Utility;



/**
  This class is repossible for managing photos for the Locker Application
 **/


public class PhotoLockManager{

	private Context context;

	/**
  Moves the Photos from the Native Gallery to the App Gallery
  Use Cases:
	  1. Check for Battery 	
	  2. Photo Access Fails for a photo - Then Skip
	  3. Photo Copy Fails, then Skip	
	  4. Photo Delete Fails
	  5. Incoming Phone Call
	This would be a AyncTask doing the following Activities:
		1. Accesses a photo in the Gallery
		2. Copies the Photo into a "App Gallery" Location
		3. Deletes the Photos from gallery
		4. Refresh Gallery
	 **/  
	private static PhotoLockManager lockManager;

	public static PhotoLockManager getInstance(){
		if (lockManager == null) {
			lockManager = new PhotoLockManager();
		}

		return lockManager;
	}



	public void movePhotos(ArrayList<String> listOfPhotos){
		Object[] list =   listOfPhotos.toArray();
		new MovePhotosTask().execute(list);	
	}

	public void setContext(Context context) {
		this.context = context;
	}

	/**
 This method shares a list of Photos with a socialMedia
	 **/
	public void sharePhotos(ArrayList list, byte socialMediaID){

	}

	/**
 Marks a Photo as Favorite
	 **/  
	public void markAsFavorite(){
	}

	/**
 Gets the List of Favorite Photos
	 **/
	public void getFavoriteList(){
	}



	private class MovePhotosTask extends AsyncTask<Object,Integer, Long> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		/**This would be a AyncTask doing the following Activities:
		 1. Accesses a photo in the Gallery
		 2. Copies the Photo into a "App Gallery" Location
		 3. Deletes the Photos from gallery
		 4. Refresh Gallery
		 **/
		protected Long doInBackground(Object... uri) {
			long totalCount = uri.length;
			int photosMoved = 0;
			File sourceFile =null;
			File destFile =null;
			while(photosMoved != totalCount){
				sourceFile = new File((String)uri[photosMoved++]);
				destFile = new File(Utility.checkcreateFolder() + "/" + sourceFile.getName());
				try {
					Utility.copyFile(sourceFile, destFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
				Utility.renameFile(destFile, Constants.EXT_PICTURES);
				Utility.deleteFile(sourceFile);

			}
			publishProgress((int) ((photosMoved/ (float) totalCount) * 100));
			return totalCount;
		}




		protected void onProgressUpdate(Integer... progress) {
			Log.i("onProgressUpdate", progress[0]+"");
		}

		protected void onPostExecute(Long result) {
			// This is to refresh the Gallery Images
			context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file:// " +
					Environment.getExternalStorageDirectory())));
		}
	}


}


